package com.wheesper.www.WheesperServer.Services;

import java.util.HashMap;
import java.util.Map;

import com.Wheesper.www.WheesperServer.Model.User;
import com.Wheesper.www.WheesperServer.Model.UserCenter;
import com.wheesper.www.WheesperServer.Common.OfflineMessageTrusteeship;
import com.wheesper.www.WheesperServer.Net.Session.IOSession;
import com.wheesper.www.WheesperServer.Utils.Logger;

import ProtocolBuffer.ProtoMessageOuterClass.ChatGroupMessagePushMessage;
import ProtocolBuffer.ProtoMessageOuterClass.ChatGroupMessageRequest;
import ProtocolBuffer.ProtoMessageOuterClass.ChatPrivateMessagePushMessage;
import ProtocolBuffer.ProtoMessageOuterClass.ChatPrivateMessageRequest;
import ProtocolBuffer.ProtoMessageOuterClass.ChatPrivateMessageResponse;
import ProtocolBuffer.ProtoMessageOuterClass.ProtoMessage;
import com.wheesper.www.WheesperServer.Net.Chat.ChatRoom;

public class ChatController {
	private static ChatController _instance = new ChatController();
	
	public static ChatController Instance() {
		return _instance;
	}
	
	private Map<Integer,ChatRoom> _groups_chatroom;
	
	private ChatController() {
		_groups_chatroom = new HashMap<Integer,ChatRoom>();
	}
	
	private void privateChatingResponse(IOSession session,String sender_email,String receiver_email) {
		ChatPrivateMessageResponse response = ChatPrivateMessageResponse.newBuilder()
				.setSenderEmail(sender_email)
				.setReceiverEmail(receiver_email)
				.setStatus(true)
				.build();
		
		ProtoMessage message = ProtoMessage.newBuilder()
				.setChatPrivateMessageResponse(response)
				.build();
		
		session.writeToRemote(message.toByteArray());
	}
	
	private void privateChatMessagePush(IOSession session,String sender_email,ChatPrivateMessageRequest request) {
		ChatPrivateMessagePushMessage message = ChatPrivateMessagePushMessage.newBuilder()
				.setSenderEmail(sender_email)
				.setMessage(request.getMessage())
				.setReceiverEmail(request.getReciiverEmail())
				.build();
		
		ProtoMessage m =ProtoMessage.newBuilder()
				.setChatPrivateMessagePushMessage(message)
				.build();
		
		if(session == null) {
			offlineMessagePush(UserCenter.Instance().UserObject(request.getReciiverEmail()),m);
			return;
		}
		
		session.writeToRemote(m.toByteArray());
	}
	
	private void offlineMessagePush(User user,ProtoMessage message) {
		OfflineMessageTrusteeship.Instance().trusteeFor(user, message);
	}

	
	public void privateChatting(IOSession session,ChatPrivateMessageRequest request) {
		
		//解析该聊天消息
		String sender_email = request.getMessage().getSenderEmail();
		String receiver_email = request.getReciiverEmail();
		//告诉Client已经收到该消息了
		privateChatingResponse(session,sender_email,receiver_email);
		
		Logger.Instance().logging("[Private Chat Message]{\n"
				+ "Sender:"+sender_email+"\n"
				+ "Receiver:" + receiver_email + "\n}\n");
		
		//装发该消息，若对方离线，则自动发送至离线托管
		IOSession receiver_session = LogController.Instance().GetIOSessionBy(UserCenter.Instance().UserObject(receiver_email));
		try {
			privateChatMessagePush(receiver_session,sender_email,request);
		}catch(Exception e) {
			Logger.Instance().logging(e.getMessage());
		}
	}
	
	public void groupChatting(IOSession session,ChatGroupMessageRequest request) {
		Integer id = request.getGroupID();
		ChatRoom chatroom;
		if((chatroom = _groups_chatroom.get(id)) != null) {
			//TODO:
		}
	}
}
